package com.turman.plyopengl

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class SampleRenderer(private val context: Context) : GLSurfaceView.Renderer {
    companion object {
        private const val POSITION_COMPONENT_COUNT = 2
        private const val BYTES_PER_FLOAT = 4
        private const val U_COLOR = "u_Color"
        private const val A_POSITION = "a_Position"
        private const val POINT_SIZE = "point_Size"
    }

    private val tableVertices: FloatArray = floatArrayOf(
        -0.5F, -0.5F,
        0.5F, 0.5F,
        -0.5F, 0.5F,

        -0.5F, -0.5F,
        0.5F, -0.5F,
        0.5F, 0.5F,
        // 中线
        -0.5F, 0F,
        0.5F, 0F,
        // 顶点
        0F, -0.25F,
        0F, 0.25F,
        //边框
        -0.5f,-0.5f,
        0.5f,-0.5f,
        0.5f,0.5f,
        -0.5f,0.5f,
        -0.5f,-0.5f
    )


    private val vertexData: FloatBuffer = ByteBuffer
        .allocateDirect(tableVertices.size * BYTES_PER_FLOAT)
        .order(ByteOrder.nativeOrder())
        .asFloatBuffer()
        .put(tableVertices)

    private var programId = 0
    private var uColorLocation = 0
    private var aPositionLocation = 0
    private var pointSizeLocation = 0

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        GLES20.glClearColor(1f, 1f, 1f, 1f)

        val vertexShaderCode = context.readStringFromRaw(R.raw.simple_vertex_shader)
        val fragmentShaderCode = context.readStringFromRaw(R.raw.simple_fragment_shader)

        val vertexShader = ShaderHelper.compileVertexShader(vertexShaderCode)
        val fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderCode)

        programId = ShaderHelper.linkProgram(vertexShader, fragmentShader)

        //调试模式下验证链接结果
        if (context.isDebugVersion()) {
            ShaderHelper.validateProgram(programId)
        }

        GLES20.glUseProgram(programId)

        uColorLocation = GLES20.glGetUniformLocation(programId, U_COLOR)
//        GLES20.glEnableVertexAttribArray(uColorLocation)
        aPositionLocation = GLES20.glGetAttribLocation(programId, A_POSITION)
//        GLES20.glEnableVertexAttribArray(aPosition)
        pointSizeLocation = GLES20.glGetAttribLocation(programId, POINT_SIZE)

        //将顶点数据绑定到shader的a_position
        vertexData.position(0)
//        vertexData.flip()
        GLES20.glVertexAttribPointer(
            aPositionLocation, //绑定到那个shader变量
            POSITION_COMPONENT_COUNT, //一个点有几个数据
            GLES20.GL_FLOAT, //类型
            false,
            0,  //间隔
            vertexData //内存数据
        )
        GLES20.glEnableVertexAttribArray(aPositionLocation) //启用该属性


    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
        GLES20.glViewport(0, 0, width, height)
    }

    override fun onDrawFrame(gl: GL10?) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        //绘制矩形
        GLES20.glUniform4f(uColorLocation, 1f, 1f, 1f, 1f) //片段着色器中的u_color设置成白色
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6) //绘制三角形，这里0是顶点数组的起始位置，6是取的点数，就是前6个点
        //绘制线
        GLES20.glUniform4f(uColorLocation, 1f, 0f, 0f, 1f)
        GLES20.glDrawArrays(GLES20.GL_LINES, 6, 2)
        //绘制点
        GLES20.glVertexAttrib1f(pointSizeLocation,10f)
        GLES20.glUniform4f(uColorLocation, 0f, 0f, 1f, 1f)
        GLES20.glDrawArrays(GLES20.GL_POINTS, 8, 1)
        GLES20.glUniform4f(uColorLocation, 1f, 0f, 0f, 1f)
        GLES20.glDrawArrays(GLES20.GL_POINTS, 9, 1)
        //边框
        GLES20.glUniform4f(uColorLocation,1f,0f,0f,1f)
        GLES20.glDrawArrays(GLES20.GL_LINE_STRIP,10,5)

    }
}